869C - The Intriguing Obsession - CodeForces Solution


combinatorics dp math *1800

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long 
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define mp make_pair
#define pii pair<ll int,ll int>
#define umap unordered_map
#define popcount(x) __builtin_popcountll(x)
#define all(v) v.begin() , v.end()
#define PI 3.141592653589793238
#define E 2.7182818284590452353602874713527
#define M 998244353
const long long INF = 1e18;
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << endl;
    err(++it, args...);
}
ll int fact[5001], invfact[5001];

ll power(ll a, ll b, const ll mod){ 
    ll x = 1, y = a; 
    while (b){ 
        if (b & 1){ 
            x *= y;
            x %= mod; 
        } 
        y *= y;
        y %= mod; 
        b >>= 1; 
    } 
    return x; 
}

ll modular_inverse(ll n, const ll mod){ 
    return power(n, mod-2, mod); 
} 
 
ll nCr(ll n, ll k, ll mod){ 
    return (fact[n]*((invfact[k]*invfact[n-k])%mod))%mod;
} 


int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
fact[0]=1;
for (int i = 1; i <=5000; i++)
{
    fact[i]=fact[i-1]*i;
    fact[i]%=M;
}

invfact[5000] = modular_inverse(fact[5000],M);
for(int i=4999;i>=0;i--) 
{
    invfact[i] = invfact[i+1]*(i+1)%M;
}
ll int a,b,c;
cin>>a>>b>>c;
if(a>b)
swap(a,b);
if(a>c)
swap(a,c);
if(b>c)
swap(b,c);
ll int ans=1,ans2=0,ans3=1;
for (int i = 0; i <=a; i++)
{
ans*=nCr(a,i,M);
ans%=M;
ans*=nCr(b,i,M);
ans%=M;
ans*=fact[i]%M;
ans%=M;
ans2+=ans;
ans2%=M;
ans=1;
}
ans3*=ans2;
ans3%=M;
ans2=0;
for (int i = 0; i <=a; i++)
{
ans*=nCr(a,i,M);
ans%=M;
ans*=nCr(c,i,M);
ans%=M;
ans*=fact[i]%M;
ans%=M;
ans2+=ans;
ans2%=M;
ans=1;
}
ans3*=ans2;
ans3%=M;
// error(ans3);
ans2=0;

// error(ans2);
for (int i = 0; i <=b; i++)
{
ans*=nCr(b,i,M);
ans%=M;
ans*=nCr(c,i,M);
ans%=M;
ans*=fact[i]%M;
ans%=M;
ans2+=ans;
ans2%=M;
ans=1;
}

ans3*=ans2;
ans3%=M;
cout<<ans3;
}


Comments

Submit
0 Comments
More Questions

1400A - String Similarity
1734E - Rectangular Congruence
1312D - Count the Arrays
424C - Magic Formulas
1730C - Minimum Notation
1730B - Meeting on the Line
1730A - Planets
302B - Eugeny and Play List
1730D - Prefixes and Suffixes
1515C - Phoenix and Towers
998A - Balloons
1734F - Zeros and Ones
1144B - Parity Alternated Deletions
92B - Binary Number
1144C - Two Shuffled Sequences
1154B - Make Them Equal
1272B - Snow Walking Robot
522B - Photo to Remember
608B - Hamming Distance Sum
1408F - Two Different
274B - Zero Tree
1726H - Mainak and the Bleeding Polygon
722A - Broken Clock
129B - Students and Shoelaces
697B - Barnicle
903D - Almost Difference
1443B - Saving the City
1215C - Swap Letters
1251C - Minimize The Integer
1494B - Berland Crossword